<?php

namespace app\admins\controller;

use think\Controller;
use Util\SysDb;

class Answer extends Controller
{
    public function quiz()
    {
        $this->db = new SysDb();
        $answer = $this->db->table('answerlist')->order("id")->lists();
        $quiz = $this->db->table('quizlist')->lists();

        $t = [];
        $k = [];
         
            foreach ($quiz as $j) {
                $c = [];
                foreach ($answer as $key => $x) {
                   
                    if ($x['answer_id'] == $j['quiz_id']) {
                        array_push($c, $x);
                        $j['replyBody'] = $c;
                    }
                }
                array_push($t, $j); 
            }
        

        exit(json_encode($t));
    }
    public function Teacherquiz()
    {
        $code=trim(input('param.code'));
        $CourseCode=trim(input('param.CourseCode'));
        $this->db = new SysDb();
        $answer = $this->db->table('answerlistone')->order("id")->lists();
        $quiz = $this->db->table('quizlistone')->where(['Code' => $code,'CourseCode' => $CourseCode])->lists();

        $t = [];
         
            foreach ($quiz as $j) {
                $c = [];
                foreach ($answer as $key => $x) {
                   
                    if ($x['answer_id'] == $j['quiz_id']) {
                        array_push($c, $x);
                        $j['replyBody'] = $c;
                    }
                }
                array_push($t, $j); 
            }
        

        exit(json_encode($t));
    }

    public function Studentquiz()
    {
        $code=trim(input('param.code'));
        $this->db = new SysDb();
        $quziid = $this->db->table('examinelist')->where(['releaseStatus' => '2'])->order("id")->lists();
        $answer = $this->db->table('answerlistone')->order("id")->lists();
        $quiz = $this->db->table('quizlistone')->lists();

        $t = [];
         foreach($quziid as $p){

            foreach ($quiz as $j) {
                if($p['CourseCode']==$j['CourseCode']){
                $c = [];
                foreach ($answer as $key => $x) {
                   
                    if ($x['answer_id'] == $j['quiz_id']) {
                        array_push($c, $x);
                        $j['replyBody'] = $c;
                    }
                }
                array_push($t, $j); }
            }
        }

        exit(json_encode($t));
    }
    public function answerinsertone()
    {
        //echo md5('123456');
        $code=trim(input('param.code'));
        $CourseCode=trim(input('param.CourseCode'));
        $img = trim(input('param.img'));
        $replyName = input('param.replyName');
        $content = input('param.content');
        $browse = trim(input('param.browse'));
        $osname = input('param.osname');
        $address = input('param.address');
        $replyBody = input('param.replyBody');
        $time = input('param.time');
        $data['img'] = $img;
        $data['replyName'] = $replyName;
        $data['Code'] = $code;
        $data['CourseCode'] = $CourseCode;
        $data['content'] = $content;
        $data['browse'] = $browse;
        $data['osname'] = $osname;
        $data['address'] = $address;
        $data['replyBody'] = $replyBody;
        $data['time']=$time;
        $data['beReplyName']='';
        $this->db = new SysDb();
        $count = $this->db->table('quizlistone')->count();
        $num=$count + 1;
        $data['quiz_id'] = 'quiz_'.$num;
        $data['id'] = $num;
        $this->db->table('quizlistone')->insert($data);
        exit(json_encode(['code' => 0, 'msg' => '留言成功']));
    }

    public function answerinsert()
    {
        //echo md5('123456');
        $img = trim(input('param.img'));
        $replyName = input('param.replyName');
        $content = input('param.content');
        $browse = trim(input('param.browse'));
        $osname = input('param.osname');
        $address = input('param.address');
        $replyBody = input('param.replyBody');
        $time = input('param.time');
        $data['img'] = $img;
        $data['replyName'] = $replyName;
        $data['content'] = $content;
        $data['browse'] = $browse;
        $data['osname'] = $osname;
        $data['address'] = $address;
        $data['replyBody'] = $replyBody;
        $data['time']=$time;
        $data['beReplyName']='';
        $this->db = new SysDb();
        $count = $this->db->table('quizlist')->count();
        $num=$count + 1;
        $data['quiz_id'] = 'quiz_'.$num;
        $data['id'] = $num;
        $this->db->table('quizlist')->insert($data);
        exit(json_encode(['code' => 0, 'msg' => '留言成功']));
    }
    public function quizinsert()
    {
        //echo md5('123456');
        $bereplyName = input('param.bereplyName');
        $quiz_id = input('quiz_id');
        $replyName = input('param.replyName');
        $content = input('param.content');
        $time = input('param.time');
        $data['replyName'] = $replyName;
        $data['content'] = $content;
        $data['time']=$time;
        $data['beReplyName']=$bereplyName;
        $data['answer_id'] = $quiz_id;
        $this->db = new SysDb();
        $count = $this->db->table('answerlist')->count();
        $counts = $this->db->table('answerlist')->where(['answer_id' => $quiz_id])->count();
        $num=$count + 1;
        $nums=$counts+1;
        $data['key_id'] = $num;
        $data['id'] = $nums; 
        $this->db->table('answerlist')->insert($data);
        exit(json_encode(['code' => 0, 'msg' => '留言成功']));
    }

    public function quizinsertone()
    {
        //echo md5('123456');
        $bereplyName = input('param.bereplyName');
        $quiz_id = input('quiz_id');
        $replyName = input('param.replyName');
        $content = input('param.content');
        $time = input('param.time');
        $data['replyName'] = $replyName;
        $data['content'] = $content;
        $data['time']=$time;
        $data['beReplyName']=$bereplyName;
        $data['answer_id'] = $quiz_id;
        $this->db = new SysDb();
        $count = $this->db->table('answerlistone')->count();
        $counts = $this->db->table('answerlistone')->where(['answer_id' => $quiz_id])->count();
        $num=$count + 1;
        $nums=$counts+1;
        $data['key_id'] = $num;
        $data['id'] = $nums; 
        $this->db->table('answerlistone')->insert($data);
        exit(json_encode(['code' => 0, 'msg' => '留言成功']));
    }
}
